const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //筛选框的值
    type:'',
    
    currentid:'',
    //id
    categoryId:'',
    //当前索引值
    index:0,
    //收藏列表的数据
    collegeList:[
     
    ],
    //当前分页页数
    page:1,
    //当前分页请求条数
    pageSize:5,
    
    isEnd:false,
    
    typelist:[]
    

  },

   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.categoryList()
   this.LikeOrVisitList()
  },
  change(e){
   
    this.setData({
      value:e.detail.value
    })
  },
  handleSelect(e){
  
   this.data.type=e.detail.type
   this.data.categoryId=e.detail.id
  },
 //触发搜索逻辑
  handleSearch(e){
  
  console.log(this.data.currentid,this.data.categoryId)
    
    //当切换类型时候清空列表
     if(this.data.currentid!=this.data.categoryId){
       console.log('test==>')
        this.setData({
          currentid:this.data.categoryId,
          collegeList:[],
          page:1,
          isEnd:false

        })
     }
   if(!this.data.isEnd){
    this.LikeOrVisitList()
   }
    
 
  },
  //取消收藏
  handleCancel(e){
   console.log('e==>',e)
   let type=e.target.dataset.type
   let id=e.target.dataset.id
   if(type=='islike'){
    


     Controller.delAction({"actionId":id}).then(res=>{
        
       this.data.collegeList.map((item,index)=>{
          if(item.actionId==id){
              console.log('index==>',index)
               this.data.collegeList.splice(index,1)
              this.setData({
                collegeList:this.data.collegeList
              })
          }
       })
       
        wx.showToast({
          title: '取消收藏成功',
          icon:'none'
        })
     })
   }
  },

  //获取分类
  categoryList(){
   
    
   Controller.categoryList({}).then(res=>{
      let baseData=res.data.baseData
        baseData.unshift( {id:'',title:'全部'})
      this.setData({
         typelist:baseData
      })
    
   })

  },

  //获取收藏列表
  LikeOrVisitList(){
    let page=this.data.page
    let pageSize=this.data.pageSize
    let categoryId=this.data.categoryId
    let contentType=1
    let type=1
    
  
    Controller.LikeOrVisitList({contentType,type,categoryId,page,pageSize}).then(res=>{
         let baseData=res.data.baseData
         
         

         this.setData({
          collegeList:this.data.collegeList.concat(baseData.list),
          isEnd:baseData.lastPage
         })
        
         console.log('collegeList==>',this.data.collegeList)
    })
  },
 


 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('触底了==>')
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.LikeOrVisitList()
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})